﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SWRPre
{
    public class U2DRelProto
    {
        private static readonly char[] DELIM = new char[] { ' ', '\t' };

        // ******************************************************************
        // ROUTINE TO INPUT 2-D REAL DATA MATRICES
        //   A IS ARRAY TO INPUT
        //   ANAME IS 24 CHARACTER DESCRIPTION OF A
        //   II IS NO. OF ROWS
        //   JJ IS NO. OF COLS
        //   K IS LAYER NO. (USED WITH NAME TO TITLE PRINTOUT --)
        //          IF K=0, NO LAYER IS PRINTED
        //          IF K<0, CROSS SECTION IS PRINTED)
        //   IN IS INPUT UNIT
        //   IOUT IS OUTPUT UNIT
        //  ******************************************************************

        // SUBROUTINE U2DREL(A,ANAME,II,JJ,K,IN,IOUT)
        public static void U2DREL(out float[,] a, string aName, int ii, int jj, InputUnit inUnit, OutputUnit outUnit)
        {


            //    SPECIFICATIONS:
            //    ------------------------------------------------------------------
            //    CHARACTER*24 ANAME
            //    DIMENSION A(JJ,II)
            //    CHARACTER*20 FMTIN
            //    CHARACTER*200 CNTRL
            //    CHARACTER*16 TEXT
            //    CHARACTER*200 FNAME
            //    DATA NUNOPN/99/
            //    INCLUDE 'openspec.inc'
            //    ------------------------------------------------------------------

            a = new float[jj, ii];

            // KC -- The second argument indicates that we want to read character data.
            //------READ ARRAY CONTROL RECORD AS CHARACTER DATA.
            //READ(IN,'(A)') CNTRL
            string cntrl = inUnit.ReadLine();

            // In this section, we try to find an indication that this is a free-format record.
            /*
      ICLOSE=0
      IFREE=1
      ICOL=1
      CALL URWORD(CNTRL,ICOL,ISTART,ISTOP,1,N,R,IOUT,IN)
             */
            InputUnit locat = null;
            InputUnit nunopn = null;
            int iClose = 0;
            int iFree = 1;
            int iCol = 1;
            int iStart, iStop;
            int r = 0;
            int n = 0;

            string fmtin = "";

            // Get the control word from the line and determine how to process the rest of the record.
            string controlWord = (string)ModflowUtil.Urword(cntrl, ref iCol, out iStart, out iStop, 1, r, outUnit, inUnit);
            if (controlWord.Equals("CONSTANT"))
            {
                locat = InputUnit.ZERO;
            }
            else if (controlWord.Equals("INTERNAL"))
            {
                locat = inUnit;
            }
            else if (controlWord.Equals("EXTERNAL"))
            {
                locat = InputUnit.GetFromInt((int)ModflowUtil.Urword(cntrl, ref iCol, out iStart, out iStop, 2, r, outUnit, inUnit));
            }
            else if (controlWord.Equals("OPEN/CLOSE"))
            {
                // CALL URWORD(CNTRL,ICOL,ISTART,ISTOP,0,N,R,IOUT,IN)
                n = (int)ModflowUtil.Urword(cntrl, ref iCol, out iStart, out iStop, 0, r, outUnit, inUnit);
                string fName = cntrl.Substring(iStart + 1, iStop - iStart + 1);
                locat = nunopn;
                outUnit.Write("1X,/1X,'OPENING FILE ON UNIT ',I4,':',/1X,A", new object[] { locat, fName });
                iClose = 1;
                // 15    FORMAT(1X,/1X,'OPENING FILE ON UNIT ',I4,':',/1X,A)
            }
            // This is the case for fixed format.
            else
            {
                iFree = 0;

                // These statements read the fixed-format array into the appropriate variables.
                // READ(CNTRL,1,ERR=500) LOCAT,CNSTNT,FMTIN,IPRN
                // 1    FORMAT(I10,F10.0,A20,I10)
            }

            // For a free-format control record, read the remaining fields.
            //      IF(IFREE.NE.0) THEN
            if (iFree != 0)
            {
                float cnstnt = (float)ModflowUtil.Urword(cntrl, ref iCol, out iStart, out iStop, 3, r, outUnit, inUnit);
                //         CALL URWORD(CNTRL,ICOL,ISTART,ISTOP,3,N,CNSTNT,IOUT,IN)
                //         IF(LOCAT.NE.0) THEN
                if (locat != InputUnit.ZERO)
                {
                    //            CALL URWORD(CNTRL,ICOL,ISTART,ISTOP,1,N,R,IOUT,IN)
                    //            FMTIN=CNTRL(ISTART:ISTOP)
                    //            IF(ICLOSE.NE.0) THEN
                    if (iClose != 0)
                    {
                        //               IF(FMTIN.EQ.'(BINARY)') THEN
                        if (fmtin.Equals("(BINARY)"))
                        {
                            //                  OPEN(UNIT=LOCAT,FILE=FNAME,FORM=FORM,ACCESS=ACCESS, ACTION=ACTION(1))
                            //               ELSE
                        }
                        else
                        {
                            //                  OPEN(UNIT=LOCAT,FILE=FNAME,ACTION=ACTION(1))
                            //               END IF
                        }
                        //            END IF
                    }
                    //            IF(LOCAT.GT.0 .AND. FMTIN.EQ.'(BINARY)') LOCAT=-LOCAT
                    if (locat.IsPositive() && fmtin.Equals("(BINARY)"))
                    {
                        locat = locat.Negate();
                    }
                    //            CALL URWORD(CNTRL,ICOL,ISTART,ISTOP,2,IPRN,R,IOUT,IN)
                    //         END IF
                }
                //      END IF
            }



            /*
    C
    C4------TEST LOCAT TO SEE HOW TO DEFINE ARRAY VALUES.
      IF(LOCAT.EQ.0) THEN
    C
    C4A-----LOCAT=0; SET ALL ARRAY VALUES EQUAL TO CNSTNT. RETURN.
        DO 80 I=1,II
        DO 80 J=1,JJ
    80   A(J,I)=CNSTNT
        IF(K.GT.0) WRITE(IOUT,2) ANAME,CNSTNT,K
    2   FORMAT(1X,/1X,A,' =',1P,G14.6,' FOR LAYER',I4)
        IF(K.LE.0) WRITE(IOUT,3) ANAME,CNSTNT
    3   FORMAT(1X,/1X,A,' =',1P,G14.6)
        RETURN
      ELSE IF(LOCAT.GT.0) THEN
    C
    C4B-----LOCAT>0; READ FORMATTED RECORDS USING FORMAT FMTIN.
        IF(K.GT.0) THEN
           WRITE(IOUT,94) ANAME,K,LOCAT,FMTIN
    94      FORMAT(1X,///11X,A,' FOR LAYER',I4,/
     1      1X,'READING ON UNIT ',I4,' WITH FORMAT: ',A)
        ELSE IF(K.EQ.0) THEN
           WRITE(IOUT,95) ANAME,LOCAT,FMTIN
    95      FORMAT(1X,///11X,A,/
     1      1X,'READING ON UNIT ',I4,' WITH FORMAT: ',A)
        ELSE
           WRITE(IOUT,96) ANAME,LOCAT,FMTIN
    96      FORMAT(1X,///11X,A,' FOR CROSS SECTION',/
     1      1X,'READING ON UNIT ',I4,' WITH FORMAT: ',A)
        END IF
        DO 100 I=1,II
        IF(FMTIN.EQ.'(FREE)') THEN
           READ(LOCAT,*) (A(J,I),J=1,JJ)
        ELSE
           READ(LOCAT,FMTIN) (A(J,I),J=1,JJ)
        END IF
    100   CONTINUE
      ELSE
    C
    C4C-----LOCAT<0; READ UNFORMATTED ARRAY VALUES.
        LOCAT=-LOCAT
        IF(K.GT.0) THEN
           WRITE(IOUT,201) ANAME,K,LOCAT
    201      FORMAT(1X,///11X,A,' FOR LAYER',I4,/
     1      1X,'READING BINARY ON UNIT ',I4)
        ELSE IF(K.EQ.0) THEN
           WRITE(IOUT,202) ANAME,LOCAT
    202      FORMAT(1X,///1X,A,/
     1      1X,'READING BINARY ON UNIT ',I4)
        ELSE
           WRITE(IOUT,203) ANAME,LOCAT
    203      FORMAT(1X,///1X,A,' FOR CROSS SECTION',/
     1      1X,'READING BINARY ON UNIT ',I4)
        END IF
        READ(LOCAT) KSTP,KPER,PERTIM,TOTIM,TEXT,NCOL,NROW,ILAY
        READ(LOCAT) A
      END IF
    C
    C5------IF CNSTNT NOT ZERO THEN MULTIPLY ARRAY VALUES BY CNSTNT.
      IF(ICLOSE.NE.0) CLOSE(UNIT=LOCAT)
      ZERO=0.
      IF(CNSTNT.EQ.ZERO) GO TO 320
      DO 310 I=1,II
      DO 310 J=1,JJ
      A(J,I)=A(J,I)*CNSTNT
    310 CONTINUE
    C
    C6------IF PRINT CODE (IPRN) >0 OR =0 THEN PRINT ARRAY VALUES.
    320 IF(IPRN.GE.0) CALL ULAPRW(A,ANAME,0,0,JJ,II,0,IPRN,IOUT)
    C
    C7------RETURN
      RETURN
    C
    C8------CONTROL RECORD ERROR.
    500 IF(K.GT.0) THEN
         WRITE(IOUT,501) ANAME,K
    501    FORMAT(1X,/1X,'ERROR READING ARRAY CONTROL RECORD FOR ',A,
     1     ' FOR LAYER',I4,':')
      ELSE
         WRITE(IOUT,502) ANAME
    502    FORMAT(1X,/1X,'ERROR READING ARRAY CONTROL RECORD FOR ',A,':')
      END IF
      WRITE(IOUT,'(1X,A)') CNTRL
      CALL USTOP(' ')
      END
             */
        }


    }
}
